﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;

namespace ChurchData.Business
{
    public class EmployeeBO : ChurchDataBusinessBase
    {
        public EmployeeBO()
        {
            this.TableName = "Employee";
        }

        #region Get Methods
        public List<EmployeeAdminView> GetActiveRecords()
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            IEnumerable<EmployeeAdminView> x =
                    from e in oDB.Employees
                    where e.ActiveInd == true
                    select new EmployeeAdminView { EmployeePK = e.EmployeePK, 
                        LastName = e.LastName, FirstName = e.FirstName };

            return x.ToList();
        }

        public Employee GetRecordByPK(int employeePK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            IEnumerable<Employee> results = from e in oDB.Employees
                    where e.EmployeePK == employeePK
                    select e;

            return results.First();
        }

        #endregion Get Methods

        #region Login Method
        public bool ValidateLogin(string userID, string password)
        {
            ChurchDataDataContext empCon = new ChurchDataDataContext(this.GetConnectionString());

            var drsEmployee = from e in empCon.Employees
                              where e.LoginID == userID
                              && e.Password == password
                              select e;

            int matchCount = drsEmployee.Count();

            if (matchCount > 0)
            {
                UserInformation.EmployeePK = drsEmployee.First().EmployeePK;
                UserInformation.UserName = drsEmployee.First().FirstName + " " + drsEmployee.First().LastName;
                UserInformation.ViewOnlyInd = drsEmployee.First().ViewOnlyInd;
                UserInformation.ClearanceLevel = drsEmployee.First().ClearanceLevel;
                return true;
            }
            else
                return false;
        }

        #endregion Login Method

        #region Update Methods
        public void ChangePassword(int employeePK, string newPassword)
        {
            ChurchDataDataContext empCon = new ChurchDataDataContext(this.GetConnectionString());

            //EmployeeLinqDataContext empCon = new EmployeeLinqDataContext(this.GetConnectionString());
            var drsEmployee = from e in empCon.Employees
                              where e.EmployeePK == employeePK
                              select e;

            var drEmployee = drsEmployee.First();
            drEmployee.Password = newPassword;
            drEmployee.LastUpdTimestamp = DateTime.Now;
            drEmployee.LastUpdUser = UserInformation.UserName;

            empCon.SubmitChanges();
        }

        public void UpdateEmployee(Employee drEmp, bool isNew)
        {
            ChurchDataDataContext empCon = new ChurchDataDataContext(this.GetConnectionString());

            Employee emp;

            if (!isNew)
            {
                emp = (from e in empCon.Employees
                       where e.EmployeePK == drEmp.EmployeePK
                       select e).Single();
            }
            else
            {
                emp = new Employee();
                empCon.Employees.InsertOnSubmit(emp);
            }

            emp.ActiveInd = drEmp.ActiveInd;
            emp.AdminInd = drEmp.AdminInd;
            emp.ClearanceLevel = drEmp.ClearanceLevel;
            emp.FirstName = drEmp.FirstName;
            emp.LastName = drEmp.LastName;
            emp.LastUpdTimestamp = DateTime.Now;
            emp.LastUpdUser = UserInformation.UserName;
            emp.LoginID = drEmp.LoginID;
            emp.Password = drEmp.Password;
            emp.ViewOnlyInd = drEmp.ViewOnlyInd;
            
            empCon.SubmitChanges();
        }

        public void SetToInactive(int employeePK)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from e in oDB.Employees
                         where e.EmployeePK == employeePK
                         select e).Single();
                x.ActiveInd = false;
                x.LastUpdUser = UserInformation.UserName;
                x.LastUpdTimestamp = DateTime.Now;
                oDB.SubmitChanges();
            }
        }

        #endregion Update Methods
    }
}
